package com.ggshily.swf.abc;

import java.io.DataInput;
import java.io.IOException;

/**
 * Namespaces have string names, represented by indices into the string array,
 * and kinds. User-defined namespaces have kind <code>CONSTANT_Namespace</code> or 
 * <code>CONSTANT_ExplicitNamespace</code> and a non-empty name. System namespaces have empty names and one of the 
 * other kinds, and provides a means for the loader to map references to these namespaces onto internal entities.
 * 
 * @author playfish_chg
 *
 */
public class ABCNamespace
{
	public static final int CONSTANT_Namespace = 0x08;
	public static final int CONSTANT_PackageNamespace = 0x16;
	public static final int CONSTANT_PackageInternalNs = 0x17;
	public static final int CONSTANT_ProtectedNamespace = 0x18;
	public static final int CONSTANT_ExplicitNamespace = 0x19;
	public static final int CONSTANT_StaticProtectedNs = 0x1A;
	public static final int CONSTANT_PrivateNs = 0x05;

	/**
	 * U8, A single byte defines the type of entry, thus identifying how the name field should be interpreted by the loader.
	 */
	public short kind;
	
	/**
	 * U30, an index into the string section of the constant pool. A value of zero denotes an empty string. 
	 */
	public int name;
	
	public String nameStr;

	public ABCNamespace()
	{

	}

	public ABCNamespace(DataInput di, String[] strings) throws IOException
	{
		kind = ABCUtil.readU8(di);
//		if(kind == CONSTANT_Namespace || kind == CONSTANT_ExplicitNamespace)
		{
			name = ABCUtil.readU30(di);
			nameStr = strings[name];
		}
	}

	public static String getName(int type)
	{
		switch(type)
		{
		case CONSTANT_Namespace:
			return "";
		case CONSTANT_PackageNamespace:
			return "public";
		case CONSTANT_PackageInternalNs:
			return "internal";
		case CONSTANT_ProtectedNamespace:
			return "protected";
		case CONSTANT_ExplicitNamespace:
			return "explicit";
		case CONSTANT_StaticProtectedNs:
			return "protected static";
		case CONSTANT_PrivateNs:
			return "private";
		default:
			System.err.println("Wrong namespace type !!!");
			break;
		}
		return "";
	}
}
